package com.wq.myspring.beantest;

import org.springframework.beans.factory.config.ConstructorArgumentValues;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;

/**
 * @PackageName: com.wq.myspring
 * @ClassName MyTest4
 * @Description: TODO
 * @Author Wenqiang
 * @Date 2021/07/16 21:56
 * @Version V1.0
 */
public class MyTest4 {
	public static void main(String[] args) {
		DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();

		//定义第一个bean
		AbstractBeanDefinition beanDefinition1 = BeanDefinitionBuilder.genericBeanDefinition().getBeanDefinition();
		beanDefinition1.setBeanClass(Train.class);
		//第一种设置构造参数的方法
		beanDefinition1.getConstructorArgumentValues().addGenericArgumentValue("001", "String");
		beanDefinition1.getConstructorArgumentValues().addGenericArgumentValue("绿皮车", "String");
		//
		beanFactory.registerBeanDefinition("train1", beanDefinition1);
		Train train1 = beanFactory.getBean("train1", Train.class);
		System.out.println("train1 =========>" + train1);


		//定义第二个bean
		AbstractBeanDefinition beanDefinition2 = BeanDefinitionBuilder.genericBeanDefinition().getBeanDefinition();
		beanDefinition2.setBeanClass(Train.class);
		//第二种设置构造参数的方法
		ConstructorArgumentValues argumentValues = new ConstructorArgumentValues(beanDefinition2.getConstructorArgumentValues());
		argumentValues.addIndexedArgumentValue(0, "002");
		argumentValues.addIndexedArgumentValue(1, "高速列车");
		beanDefinition2.setConstructorArgumentValues(argumentValues);
		//
		beanFactory.registerBeanDefinition("train2", beanDefinition2);
		Train train2 = beanFactory.getBean("train2", Train.class);
		System.out.println("train2 =========>" + train2);

	}

}

class Train {

	private String id;
	private String name;

	public Train() {
	}

	public Train(String id, String name) {
		this.id = id;
		this.name = name;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Override
	public String toString() {
		return "[" +hashCode() + "] Train{" +
				"id='" + id + '\'' +
				", name='" + name + '\'' +
				'}';
	}
}
